package com.example.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.product.entity.Goods;
import com.example.product.entity.GoodsStock;
import com.example.product.mapper.GoodsStockMapper;
import com.example.product.service.GoodsService;
import com.example.product.service.GoodsStockService;
import com.example.product.util.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * 管理员业务处理
 **/
@Service
@Transactional
public class GoodsStockServiceImpl extends ServiceImpl<GoodsStockMapper, GoodsStock> implements GoodsStockService {

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private GoodsStockMapper goodsStockMapper;

    @Resource
    private GoodsService goodsService;


//    private Account convertGoodsStockToAccount(GoodsStock goodsStock) {
//        Account account = new Account();
//        account.setId(goodsStock.getId());
//        account.setUsername(goodsStock.getUsername());
//        account.setName(goodsStock.getName());
//        account.setRole(goodsStock.getRole());
//        account.setImg(goodsStock.getImg());
//        return account;
//    }

    @Override
    /**
     * 新增
     */
    public void add(GoodsStock goodsStock) {
        Integer num = goodsStock.getNum();
        //查询到当前进货的商品信息
        Goods goods = goodsService.selectById(goodsStock.getGoodsId());
        goods.setStore(goods.getStore() + num); //进货后的农产品数量
        goodsService.updateById(goods); //更新数据
        goodsStockMapper.insert(goodsStock);
    }

    /**
     * 删除
     */
    @Override
    public void deleteById(Integer id) {
        goodsStockMapper.deleteById(id);
    }

    @Override
    public boolean saveBatch(Collection<GoodsStock> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<GoodsStock> entityList, int batchSize) {
        return false;
    }

    /**
     * 修改
     */
    @Override
    public boolean updateById(GoodsStock goodsStock) {
        goodsStockMapper.updateById(goodsStock);
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<GoodsStock> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(GoodsStock entity) {
        return false;
    }

    @Override
    public GoodsStock getOne(Wrapper<GoodsStock> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<GoodsStock> getOneOpt(Wrapper<GoodsStock> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<GoodsStock> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<GoodsStock> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public GoodsStockMapper getBaseMapper() {
        return null;
    }

    @Override
    public Class<GoodsStock> getEntityClass() {
        return null;
    }

    /**
     * 根据ID查询
     */
    @Override
    public GoodsStock selectById(Integer id) {
        return goodsStockMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    @Override
    public List<GoodsStock> selectAll(GoodsStock goodsStock) {
        return goodsStockMapper.selectAll(goodsStock);
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<GoodsStock> selectPage(GoodsStock goodsStock, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsStock> list = goodsStockMapper.selectAll(goodsStock);
        return PageInfo.of(list);
    }


}